package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.tu.loadingdialog.LoadingDailog;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.widget.CustomPopupWindow;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.di.component.DaggerUserLoginComponent;
import com.kmproject.haifu.mvp.contract.UserLoginContract;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.presenter.UserLoginPresenter;
import com.xw.repo.XEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;
//import cn.sharesdk.framework.Platform;

/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/20/2019 21:06
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class UserLoginActivity extends BaseActivity<UserLoginPresenter> implements UserLoginContract.View {

    @BindView(R.id.userLogin_cancel_img)
    ImageView userLoginCancelImg;
    @BindView(R.id.user_phone_Attribution_txt)
    TextView userPhoneAttributionTxt;
    @BindView(R.id.user_phone_txt)
    XEditText userPhoneTxt;
    @BindView(R.id.user_pwd_txt)
    XEditText userPwdTxt;
    @BindView(R.id.user_login_btn)
    TextView userLoginBtn;
    @BindView(R.id.user_regist_txt)
    TextView userRegistTxt;
    @BindView(R.id.user_RetrievePwd_txt)
    TextView userRetrievePwdTxt;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerUserLoginComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_user_login; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        UserBean userBean=DataHelper.getDeviceData(this,AppConstant.USER_INFO);
         if (userBean==null||TextUtils.isEmpty(userBean.getMobile()))
         {
             userLoginBtn.setEnabled(false);
         }else {
             userLoginBtn.setEnabled(true);
             //显示的时候去掉63和86
             userPhoneTxt.setText(userBean.getMobile().substring(2,userBean.getMobile().length()));
         }
        if (userPhoneTxt.getText().toString().length()>0&&userPhoneTxt.getText().toString().startsWith("1"))
        {
            userPhoneAttributionTxt.setText("+86");
        }else {
            userPhoneAttributionTxt.setText("+63");
        }
        userPhoneTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                   if (!TextUtils.isEmpty(s)&&!TextUtils.isEmpty(userPwdTxt.getText().toString()))
                   {
                       setBtnStatus(true);
                   }else {
                       setBtnStatus(false);
                   }
            }
        });

        userPwdTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().contains(" "))
                {
                    setBtnStatus(false);
                    return;
                }
                if (!TextUtils.isEmpty(s)&&!TextUtils.isEmpty(userPhoneTxt.getText().toString())&&s.toString().length()>7)
                {
                    setBtnStatus(true);
                }else {
                    setBtnStatus(false);
                }
            }
        });

    }
    private void showShare() {


//        OnekeyShareyShare oks = new OnekeyShare();
//        oks.disableSSOWhenAuthorize(); //关闭sso授权
//        Platform platform = App.getInstance().getPlatformList().get(0);
//        if (platform != null) {
//            ShareTypeManager shareManager = new ShareTypeManager(this, platform);
//            shareManager.shareShow(platformCode, this);
//        }
    }
    /**
     * 设置登录按钮状态
     * @param isClick
     */
      public void setBtnStatus(boolean isClick)
      {
          if (userLoginBtn==null)
          {
              return;
          }
          if (isClick)
          {
              userLoginBtn.setEnabled(true);
              userLoginBtn.setBackgroundResource(R.drawable.button_yellow_bg);
              userLoginBtn.setTextColor(getResources().getColor(R.color.color_191919));
          }else {
              userLoginBtn.setEnabled(false);
              userLoginBtn.setBackgroundResource(R.drawable.button_gray_bg);
              userLoginBtn.setTextColor(getResources().getColor(R.color.color_EDEDED));
          }
      }
    @Override
    public void showLoading() {
        loadingDialog.show();
    }

    @Override
    public void hideLoading() {
          loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.userLogin_cancel_img,R.id.user_phone_Attribution_img, R.id.user_phone_Attribution_txt, R.id.user_login_btn, R.id.user_regist_txt, R.id.user_RetrievePwd_txt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.userLogin_cancel_img:
                killMyself();
                break;
            case R.id.user_phone_Attribution_txt:
                showPopuWindow();
                break;
            case R.id.user_phone_Attribution_img:
                showPopuWindow();
                break;
            case R.id.user_login_btn:
//                showShare();

                if (userPhoneAttributionTxt.getText().toString().equals("+63"))
                {
                    if (userPhoneTxt.getText().toString().substring(0,1).equals("0"))
                    {
                        showMessage("手机号不能是0开头");
                        return;
                    }
                    if (!userPhoneTxt.getText().toString().substring(0,1).equals("9"))
                    {
                        showMessage("手机号必须是9开头");
                        return;
                    }
                    if (userPhoneTxt.getText().toString().length()!=10)
                    {
                        showMessage("手机号长度必须是十位");
                        return;
                    }
                    mPresenter.userLogin("63"+userPhoneTxt.getText().toString().trim(),userPwdTxt.getText().toString().trim());
                }else {
                    if (userPhoneTxt.getText().toString().length()!=11)
                    {
                        showMessage("手机号必须是11位");
                        return;
                    }
                    mPresenter.userLogin("86"+userPhoneTxt.getText().toString().trim(),userPwdTxt.getText().toString().trim());
                }
                break;
            case R.id.user_regist_txt:
                ArmsUtils.startActivityForResult(UserRegistActivity.class,1001);
                break;
            case R.id.user_RetrievePwd_txt:

                Intent intent=new Intent(this,UserForgetPwdActivity.class);
                intent.putExtra("mobile",userPhoneTxt.getText().toString());
                ArmsUtils.startActivity(intent);

                break;
        }
    }

    /**
     * 控制popu显示
     */
    public void showPopuWindow(){
        CustomPopupWindow customPopupWindow = new CustomPopupWindow.Builder().contentView(CustomPopupWindow.inflateView(this, R.layout.user_popuwindow_view)).parentView(userPhoneAttributionTxt).backgroundDrawable(getResources().getDrawable(R.drawable.button_white_bg))
                .animationStyle(R.style.mypopwindow_anim_style).customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {

                    }
                }).isWrap(true).isHaveBackGround(true).build();
        View view1 = customPopupWindow.getContentView();
        TextView user_popuwindow_view_flb = view1.findViewById(R.id.user_popuwindow_view_flb);
        TextView user_popuwindow_view_zg = view1.findViewById(R.id.user_popuwindow_view_zg);
        user_popuwindow_view_flb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userPhoneAttributionTxt.setText("+63");
                customPopupWindow.dismiss();
            }
        });
        user_popuwindow_view_zg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userPhoneAttributionTxt.setText("+86");
                customPopupWindow.dismiss();
            }
        });
        customPopupWindow.show();
    }



    @Override
    public void qurUserInfoSuccess(UserBean userBean) {
        ArmsUtils.startActivity(HomeActivity.class);
        killMyself();
    }

    @Override
    public LoadingDailog getLoadingDailog() {
        return loadingDialog;
    }
}
